
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>模式 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../../../../0/style.css">

    
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/search.css">
                
            
                
                <link rel="stylesheet" href="../../../../../../0/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="09_Generic_Parameters_and_Arguments.html" />
    
    
    <link rel="prev" href="07_Attributes.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    欢迎使用 Swift
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/01_about_swift.html">
            
                <a href="../chapter1/01_about_swift.html">
            
                    
                    关于 Swift
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/02_version_compatibility.html">
            
                <a href="../chapter1/02_version_compatibility.html">
            
                    
                    版本兼容性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/03_a_swift_tour.html">
            
                <a href="../chapter1/03_a_swift_tour.html">
            
                    
                    Swift 初见
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/04_revision_history.html">
            
                <a href="../chapter1/04_revision_history.html">
            
                    
                    Swift 版本历史记录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    Swift 教程
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/01_The_Basics.html">
            
                <a href="../chapter2/01_The_Basics.html">
            
                    
                    基础部分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/02_Basic_Operators.html">
            
                <a href="../chapter2/02_Basic_Operators.html">
            
                    
                    基本运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/03_Strings_and_Characters.html">
            
                <a href="../chapter2/03_Strings_and_Characters.html">
            
                    
                    字符串和字符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/04_Collection_Types.html">
            
                <a href="../chapter2/04_Collection_Types.html">
            
                    
                    集合类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/05_Control_Flow.html">
            
                <a href="../chapter2/05_Control_Flow.html">
            
                    
                    控制流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../chapter2/06_Functions.html">
            
                <a href="../chapter2/06_Functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../chapter2/07_Closures.html">
            
                <a href="../chapter2/07_Closures.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../chapter2/08_Enumerations.html">
            
                <a href="../chapter2/08_Enumerations.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../chapter2/09_Structures_And_Classes.html">
            
                <a href="../chapter2/09_Structures_And_Classes.html">
            
                    
                    类和结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../chapter2/10_Properties.html">
            
                <a href="../chapter2/10_Properties.html">
            
                    
                    属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../chapter2/11_Methods.html">
            
                <a href="../chapter2/11_Methods.html">
            
                    
                    方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../chapter2/12_Subscripts.html">
            
                <a href="../chapter2/12_Subscripts.html">
            
                    
                    下标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../chapter2/13_Inheritance.html">
            
                <a href="../chapter2/13_Inheritance.html">
            
                    
                    继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../chapter2/14_Initialization.html">
            
                <a href="../chapter2/14_Initialization.html">
            
                    
                    构造过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="../chapter2/15_Deinitialization.html">
            
                <a href="../chapter2/15_Deinitialization.html">
            
                    
                    析构过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="../chapter2/16_Optional_Chaining.html">
            
                <a href="../chapter2/16_Optional_Chaining.html">
            
                    
                    可选链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="../chapter2/17_Error_Handling.html">
            
                <a href="../chapter2/17_Error_Handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="../chapter2/18_Type_Casting.html">
            
                <a href="../chapter2/18_Type_Casting.html">
            
                    
                    类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="../chapter2/19_Nested_Types.html">
            
                <a href="../chapter2/19_Nested_Types.html">
            
                    
                    嵌套类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="../chapter2/20_Extensions.html">
            
                <a href="../chapter2/20_Extensions.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="../chapter2/21_Protocols.html">
            
                <a href="../chapter2/21_Protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="../chapter2/22_Generics.html">
            
                <a href="../chapter2/22_Generics.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="../chapter2/23_Automatic_Reference_Counting.html">
            
                <a href="../chapter2/23_Automatic_Reference_Counting.html">
            
                    
                    自动引用计数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.24" data-path="../chapter2/24_Memory_Safety.html">
            
                <a href="../chapter2/24_Memory_Safety.html">
            
                    
                    内存安全
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.25" data-path="../chapter2/25_Access_Control.html">
            
                <a href="../chapter2/25_Access_Control.html">
            
                    
                    访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.26" data-path="../chapter2/26_Advanced_Operators.html">
            
                <a href="../chapter2/26_Advanced_Operators.html">
            
                    
                    高级运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    语言参考
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="01_About_the_Language_Reference.html">
            
                <a href="01_About_the_Language_Reference.html">
            
                    
                    关于语言参考
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="02_Lexical_Structure.html">
            
                <a href="02_Lexical_Structure.html">
            
                    
                    词法结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="03_Types.html">
            
                <a href="03_Types.html">
            
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="04_Expressions.html">
            
                <a href="04_Expressions.html">
            
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="05_Statements.html">
            
                <a href="05_Statements.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="06_Declarations.html">
            
                <a href="06_Declarations.html">
            
                    
                    声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="07_Attributes.html">
            
                <a href="07_Attributes.html">
            
                    
                    特性
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.8" data-path="08_Patterns.html">
            
                <a href="08_Patterns.html">
            
                    
                    模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="09_Generic_Parameters_and_Arguments.html">
            
                <a href="09_Generic_Parameters_and_Arguments.html">
            
                    
                    泛型参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="10_Summary_of_the_Grammar.html">
            
                <a href="10_Summary_of_the_Grammar.html">
            
                    
                    语法总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    翻译贡献者
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../contributors.html">
            
                <a href="../contributors.html">
            
                    
                    翻译贡献者
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >模式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6A21;&#x5F0F;&#xFF08;patterns&#xFF09;">&#x6A21;&#x5F0F;&#xFF08;Patterns&#xFF09;</h1>
<p><em>&#x6A21;&#x5F0F;</em>&#x4EE3;&#x8868;&#x5355;&#x4E2A;&#x503C;&#x6216;&#x8005;&#x590D;&#x5408;&#x503C;&#x7684;&#x7ED3;&#x6784;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5143;&#x7EC4; <code>(1, 2)</code> &#x7684;&#x7ED3;&#x6784;&#x662F;&#x7531;&#x9017;&#x53F7;&#x5206;&#x9694;&#x7684;&#xFF0C;&#x5305;&#x542B;&#x4E24;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x5217;&#x8868;&#x3002;&#x56E0;&#x4E3A;&#x6A21;&#x5F0F;&#x4EE3;&#x8868;&#x4E00;&#x79CD;&#x503C;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7279;&#x5B9A;&#x7684;&#x67D0;&#x4E2A;&#x503C;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x6A21;&#x5F0F;&#x6765;&#x5339;&#x914D;&#x5404;&#x79CD;&#x5404;&#x6837;&#x7684;&#x503C;&#x3002;&#x6BD4;&#x5982;&#xFF0C;<code>(x, y)</code> &#x53EF;&#x4EE5;&#x5339;&#x914D;&#x5143;&#x7EC4; <code>(1, 2)</code>&#xFF0C;&#x4EE5;&#x53CA;&#x4EFB;&#x4F55;&#x542B;&#x4E24;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x5143;&#x7EC4;&#x3002;&#x9664;&#x4E86;&#x5229;&#x7528;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x4E00;&#x4E2A;&#x503C;&#x4EE5;&#x5916;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4ECE;&#x590D;&#x5408;&#x503C;&#x4E2D;&#x63D0;&#x53D6;&#x51FA;&#x90E8;&#x5206;&#x6216;&#x5168;&#x90E8;&#x503C;&#xFF0C;&#x7136;&#x540E;&#x5206;&#x522B;&#x628A;&#x5404;&#x4E2A;&#x90E8;&#x5206;&#x7684;&#x503C;&#x548C;&#x4E00;&#x4E2A;&#x5E38;&#x91CF;&#x6216;&#x53D8;&#x91CF;&#x7ED1;&#x5B9A;&#x8D77;&#x6765;&#x3002;</p>
<p>Swift &#x4E2D;&#x7684;&#x6A21;&#x5F0F;&#x5206;&#x4E3A;&#x4E24;&#x7C7B;&#xFF1A;&#x4E00;&#x79CD;&#x80FD;&#x6210;&#x529F;&#x5339;&#x914D;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF0C;&#x53E6;&#x4E00;&#x79CD;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x5339;&#x914D;&#x67D0;&#x4E2A;&#x7279;&#x5B9A;&#x503C;&#x65F6;&#x53EF;&#x80FD;&#x4F1A;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x7B2C;&#x4E00;&#x7C7B;&#x6A21;&#x5F0F;&#x7528;&#x4E8E;&#x89E3;&#x6784;&#x7B80;&#x5355;&#x53D8;&#x91CF;&#x3001;&#x5E38;&#x91CF;&#x548C;&#x53EF;&#x9009;&#x7ED1;&#x5B9A;&#x4E2D;&#x7684;&#x503C;&#x3002;&#x6B64;&#x7C7B;&#x6A21;&#x5F0F;&#x5305;&#x62EC;&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5F0F;&#x3001;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#xFF0C;&#x4EE5;&#x53CA;&#x5305;&#x542B;&#x524D;&#x4E24;&#x79CD;&#x6A21;&#x5F0F;&#x7684;&#x503C;&#x7ED1;&#x5B9A;&#x6A21;&#x5F0F;&#x548C;&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4E3A;&#x8FD9;&#x7C7B;&#x6A21;&#x5F0F;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#xFF0C;&#x4ECE;&#x800C;&#x9650;&#x5236;&#x5B83;&#x4EEC;&#x53EA;&#x80FD;&#x5339;&#x914D;&#x67D0;&#x79CD;&#x7279;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x7C7B;&#x6A21;&#x5F0F;&#x7528;&#x4E8E;&#x5168;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x4F60;&#x8BD5;&#x56FE;&#x5339;&#x914D;&#x7684;&#x503C;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x53EF;&#x80FD;&#x4E0D;&#x5B58;&#x5728;&#x3002;&#x6B64;&#x7C7B;&#x6A21;&#x5F0F;&#x5305;&#x62EC;&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;&#x3001;&#x53EF;&#x9009;&#x6A21;&#x5F0F;&#x3001;&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;&#x548C;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x6A21;&#x5F0F;&#x3002;&#x4F60;&#x5728; <code>switch</code> &#x8BED;&#x53E5;&#x7684; <code>case</code> &#x6807;&#x7B7E;&#x4E2D;&#xFF0C;<code>do</code> &#x8BED;&#x53E5;&#x7684; <code>catch</code> &#x5B50;&#x53E5;&#x4E2D;&#xFF0C;&#x6216;&#x8005;&#x5728; <code>if</code>&#x3001;<code>while</code>&#x3001;<code>guard</code> &#x548C; <code>for-in</code> &#x8BED;&#x53E5;&#x7684; <code>case</code> &#x6761;&#x4EF6;&#x53E5;&#x4E2D;&#x4F7F;&#x7528;&#x8FD9;&#x7C7B;&#x6A21;&#x5F0F;&#x3002;</p>
<blockquote>
<p>&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="pattern">pattern </h4>
<blockquote>
<p><em>&#x6A21;&#x5F0F;</em> &#x2192; <a href="#wildcard_pattern"><em>&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5F0F;</em></a> <a href="03_Types.html#type-annotation"><em>&#x7C7B;&#x578B;&#x6CE8;&#x89E3;</em></a><sub>&#x53EF;&#x9009;</sub></p>
<p><em>&#x6A21;&#x5F0F;</em> &#x2192; <a href="#identifier_pattern"><em>&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;</em></a> <a href="03_Types.html#type-annotation"><em>&#x7C7B;&#x578B;&#x6CE8;&#x89E3;</em></a><sub>&#x53EF;&#x9009;</sub></p>
<p><em>&#x6A21;&#x5F0F;</em> &#x2192; <a href="#value-binding-pattern"><em>&#x503C;&#x7ED1;&#x5B9A;&#x6A21;&#x5F0F;</em></a></p>
<p><em>&#x6A21;&#x5F0F;</em> &#x2192; <a href="#tuple-pattern"><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;</em></a> <a href="03_Types.html#type-annotation"><em>&#x7C7B;&#x578B;&#x6CE8;&#x89E3;</em></a><sub>&#x53EF;&#x9009;</sub></p>
<p><em>&#x6A21;&#x5F0F;</em> &#x2192; <a href="#enum-case-pattern"><em>&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;</em></a></p>
<p><em>&#x6A21;&#x5F0F;</em> &#x2192; <a href="#optional-pattern"><em>&#x53EF;&#x9009;&#x6A21;&#x5F0F;</em></a></p>
<p><em>&#x6A21;&#x5F0F;</em> &#x2192; <a href="#type-casting-pattern"><em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x6A21;&#x5F0F;</em></a></p>
<p><em>&#x6A21;&#x5F0F;</em> &#x2192; <a href="#expression-pattern"><em>&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;</em></a></p>
</blockquote>
<h2 id="wildcard-pattern">&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5F0F;&#xFF08;Wildcard Pattern&#xFF09; </h2>
<p><em>&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5F0F;</em>&#x7531;&#x4E00;&#x4E2A;&#x4E0B;&#x5212;&#x7EBF;&#xFF08;<code>_</code>&#xFF09;&#x6784;&#x6210;&#xFF0C;&#x7528;&#x4E8E;&#x5339;&#x914D;&#x5E76;&#x5FFD;&#x7565;&#x4EFB;&#x4F55;&#x503C;&#x3002;&#x5F53;&#x4F60;&#x60F3;&#x5FFD;&#x7565;&#x88AB;&#x5339;&#x914D;&#x7684;&#x503C;&#x65F6;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8BE5;&#x6A21;&#x5F0F;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5728;&#x95ED;&#x533A;&#x95F4; <code>1...3</code> &#x4E2D;&#x8FED;&#x4EE3;&#xFF0C;&#x6BCF;&#x6B21;&#x8FED;&#x4EE3;&#x90FD;&#x5FFD;&#x7565;&#x8BE5;&#x533A;&#x95F4;&#x7684;&#x5F53;&#x524D;&#x503C;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">for</span> <span class="hljs-number">_</span> <span class="hljs-keyword">in</span> <span class="hljs-number">1</span>...<span class="hljs-number">3</span> {
    <span class="hljs-comment">// ...</span>
}
</code></pre>
<blockquote>
<p>&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="wildcard-pattern">wildcard-pattern </h4>
<blockquote>
<p><em>&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5F0F;</em> &#x2192; <strong>_</strong></p>
</blockquote>
<h2 id="identifier-pattern">&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#xFF08;Identifier Pattern&#xFF09; </h2>
<p><em>&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;</em>&#x5339;&#x914D;&#x4EFB;&#x4F55;&#x503C;&#xFF0C;&#x5E76;&#x5C06;&#x5339;&#x914D;&#x7684;&#x503C;&#x548C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6216;&#x5E38;&#x91CF;&#x7ED1;&#x5B9A;&#x8D77;&#x6765;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x5E38;&#x91CF;&#x58F0;&#x660E;&#x4E2D;&#xFF0C;<code>someValue</code> &#x662F;&#x4E00;&#x4E2A;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#xFF0C;&#x5339;&#x914D;&#x4E86; <code>Int</code> &#x7C7B;&#x578B;&#x7684; <code>42</code>&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> someValue = <span class="hljs-number">42</span>
</code></pre>
<p>&#x5F53;&#x5339;&#x914D;&#x6210;&#x529F;&#x65F6;&#xFF0C;<code>42</code> &#x88AB;&#x7ED1;&#x5B9A;&#xFF08;&#x8D4B;&#x503C;&#xFF09;&#x7ED9;&#x5E38;&#x91CF; <code>someValue</code>&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6216;&#x5E38;&#x91CF;&#x58F0;&#x660E;&#x7684;&#x5DE6;&#x8FB9;&#x662F;&#x4E00;&#x4E2A;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#x662F;&#x503C;&#x7ED1;&#x5B9A;&#x6A21;&#x5F0F;&#x7684;&#x5B50;&#x6A21;&#x5F0F;&#x3002;</p>
<blockquote>
<p>&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="identifier-pattern">identifier-pattern </h4>
<blockquote>
<p><em>&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;</em> &#x2192; <a href="02_Lexical_Structure.html#identifier"><em>&#x6807;&#x8BC6;&#x7B26;</em></a></p>
</blockquote>
<h2 id="value-binding-pattern">&#x503C;&#x7ED1;&#x5B9A;&#x6A21;&#x5F0F;&#xFF08;Value-Binding Pattern&#xFF09; </h2>
<p><em>&#x503C;&#x7ED1;&#x5B9A;&#x6A21;&#x5F0F;</em>&#x628A;&#x5339;&#x914D;&#x5230;&#x7684;&#x503C;&#x7ED1;&#x5B9A;&#x7ED9;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6216;&#x5E38;&#x91CF;&#x3002;&#x628A;&#x5339;&#x914D;&#x5230;&#x7684;&#x503C;&#x7ED1;&#x5B9A;&#x7ED9;&#x5E38;&#x91CF;&#x65F6;&#xFF0C;&#x7528;&#x5173;&#x952E;&#x5B57; <code>let</code>&#xFF0C;&#x7ED1;&#x5B9A;&#x7ED9;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x7528;&#x5173;&#x952E;&#x5B57; <code>var</code>&#x3002;</p>
<p>&#x5728;&#x503C;&#x7ED1;&#x5B9A;&#x6A21;&#x5F0F;&#x4E2D;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#x4F1A;&#x628A;&#x65B0;&#x547D;&#x540D;&#x7684;&#x53D8;&#x91CF;&#x6216;&#x5E38;&#x91CF;&#x4E0E;&#x5339;&#x914D;&#x5230;&#x7684;&#x503C;&#x505A;&#x7ED1;&#x5B9A;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x62C6;&#x5F00;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;&#xFF0C;&#x7136;&#x540E;&#x628A;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x7ED1;&#x5B9A;&#x5230;&#x76F8;&#x5E94;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#x4E2D;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> point = (<span class="hljs-number">3</span>, <span class="hljs-number">2</span>)
<span class="hljs-keyword">switch</span> point {
<span class="hljs-comment">// &#x5C06; point &#x4E2D;&#x7684;&#x5143;&#x7D20;&#x7ED1;&#x5B9A;&#x5230; x &#x548C; y</span>
<span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> (x, y):
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The point is at (<span class="hljs-subst">\(x)</span>, <span class="hljs-subst">\(y)</span>).&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;The point is at (3, 2).&#x201D;</span>
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>let</code> &#x4F1A;&#x5206;&#x914D;&#x5230;&#x5143;&#x7EC4;&#x6A21;&#x5F0F; <code>(x, y)</code> &#x4E2D;&#x7684;&#x5404;&#x4E2A;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#x3002;&#x56E0;&#x6B64;&#xFF0C;<code>switch</code> &#x8BED;&#x53E5;&#x4E2D; <code>case let (x, y):</code> &#x548C; <code>case (let x, let y):</code> &#x7684;&#x5339;&#x914D;&#x6548;&#x679C;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;</p>
<blockquote>
<p>&#x503C;&#x7ED1;&#x5B9A;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="value-binding-pattern">value-binding-pattern </h4>
<blockquote>
<p><em>&#x503C;&#x7ED1;&#x5B9A;&#x6A21;&#x5F0F;</em> &#x2192; <strong>var</strong> <a href="#pattern"><em>&#x6A21;&#x5F0F;</em></a> | <strong>let</strong> <a href="#pattern"><em>&#x6A21;&#x5F0F;</em></a></p>
</blockquote>
<h2 id="tuple-pattern">&#x5143;&#x7EC4;&#x6A21;&#x5F0F; </h2>
<p><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;</em>&#x662F;&#x7531;&#x9017;&#x53F7;&#x5206;&#x9694;&#x7684;&#xFF0C;&#x5177;&#x6709;&#x96F6;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x6A21;&#x5F0F;&#x7684;&#x5217;&#x8868;&#xFF0C;&#x5E76;&#x7531;&#x4E00;&#x5BF9;&#x5706;&#x62EC;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x3002;&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x5339;&#x914D;&#x76F8;&#x5E94;&#x5143;&#x7EC4;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x6CE8;&#x89E3;&#x53BB;&#x9650;&#x5236;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x80FD;&#x5339;&#x914D;&#x54EA;&#x79CD;&#x5143;&#x7EC4;&#x7C7B;&#x578B;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x5E38;&#x91CF;&#x58F0;&#x660E; <code>let (x, y): (Int, Int) = (1, 2)</code> &#x4E2D;&#x7684;&#x5143;&#x7EC4;&#x6A21;&#x5F0F; <code>(x, y): (Int, Int)</code> &#x53EA;&#x5339;&#x914D;&#x4E24;&#x4E2A;&#x5143;&#x7D20;&#x90FD;&#x662F; <code>Int</code> &#x7C7B;&#x578B;&#x7684;&#x5143;&#x7EC4;&#x3002;</p>
<p>&#x5F53;&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x88AB;&#x7528;&#x4E8E; <code>for-in</code> &#x8BED;&#x53E5;&#x6216;&#x8005;&#x53D8;&#x91CF;&#x548C;&#x5E38;&#x91CF;&#x58F0;&#x660E;&#x65F6;&#xFF0C;&#x5B83;&#x4EC5;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x901A;&#x914D;&#x7B26;&#x6A21;&#x5F0F;&#x3001;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#x3001;&#x53EF;&#x9009;&#x6A21;&#x5F0F;&#x6216;&#x8005;&#x5176;&#x4ED6;&#x5305;&#x542B;&#x8FD9;&#x4E9B;&#x6A21;&#x5F0F;&#x7684;&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x3002;&#x6BD4;&#x5982;&#x4E0B;&#x9762;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x5C31;&#x4E0D;&#x6B63;&#x786E;&#xFF0C;&#x56E0;&#x4E3A; <code>(x, 0)</code> &#x4E2D;&#x7684;&#x5143;&#x7D20; <code>0</code> &#x662F;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> points = [(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">1</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">1</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">2</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">2</span>, <span class="hljs-number">1</span>)]
<span class="hljs-comment">// &#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x9519;&#x8BEF;&#x7684;</span>
<span class="hljs-keyword">for</span> (x, <span class="hljs-number">0</span>) <span class="hljs-keyword">in</span> points {
    <span class="hljs-comment">/* ... */</span>
}
</code></pre>
<p>&#x53EA;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x7684;&#x5706;&#x62EC;&#x53F7;&#x6CA1;&#x6709;&#x6548;&#x679C;&#xFF0C;&#x6A21;&#x5F0F;&#x53EA;&#x5339;&#x914D;&#x8FD9;&#x4E2A;&#x5355;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x8BED;&#x53E5;&#x662F;&#x7B49;&#x6548;&#x7684;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> a = <span class="hljs-number">2</span>        <span class="hljs-comment">// a: Int = 2</span>
<span class="hljs-keyword">let</span> (a) = <span class="hljs-number">2</span>      <span class="hljs-comment">// a: Int = 2</span>
<span class="hljs-keyword">let</span> (a): <span class="hljs-type">Int</span> = <span class="hljs-number">2</span> <span class="hljs-comment">// a: Int = 2</span>
</code></pre>
<blockquote>
<p>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="tuple-pattern">tuple-pattern </h4>
<blockquote>
<p><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;</em> &#x2192; <strong>(</strong> <a href="#tuple-pattern-element-list"><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x5143;&#x7D20;&#x5217;&#x8868;</em></a><sub>&#x53EF;&#x9009;</sub> <strong>)</strong></p>
</blockquote>
<h4 id="tuple-pattern-element-list">tuple-pattern-element-list </h4>
<blockquote>
<p><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x5143;&#x7D20;&#x5217;&#x8868;</em> &#x2192; <a href="#tuple-pattern-element"><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x5143;&#x7D20;</em></a> | <a href="#tuple-pattern-element"><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x5143;&#x7D20;</em></a>  <strong>,</strong> <a href="#tuple-pattern-element-list"><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x5143;&#x7D20;&#x5217;&#x8868;</em></a></p>
</blockquote>
<h4 id="tuple-pattern-element">tuple-pattern-element </h4>
<blockquote>
<p><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x5143;&#x7D20;</em> &#x2192; <a href="#pattern"><em>&#x6A21;&#x5F0F;</em></a></p>
</blockquote>
<h2 id="enumeration-case-pattern">&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;&#xFF08;Enumeration Case Pattern&#xFF09; </h2>
<p><em>&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;</em>&#x5339;&#x914D;&#x73B0;&#x6709;&#x7684;&#x67D0;&#x4E2A;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x7684;&#x67D0;&#x4E2A;&#x7528;&#x4F8B;&#x3002;&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;&#x51FA;&#x73B0;&#x5728; <code>switch</code> &#x8BED;&#x53E5;&#x4E2D;&#x7684; <code>case</code> &#x6807;&#x7B7E;&#x4E2D;&#xFF0C;&#x4EE5;&#x53CA; <code>if</code>&#x3001;<code>while</code>&#x3001;<code>guard</code> &#x548C; <code>for-in</code> &#x8BED;&#x53E5;&#x7684; <code>case</code> &#x6761;&#x4EF6;&#x4E2D;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x51C6;&#x5907;&#x5339;&#x914D;&#x7684;&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6709;&#x4EFB;&#x4F55;&#x5173;&#x8054;&#x7684;&#x503C;&#xFF0C;&#x5219;&#x76F8;&#x5E94;&#x7684;&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;&#x5FC5;&#x987B;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x6BCF;&#x4E2A;&#x5173;&#x8054;&#x503C;&#x5143;&#x7D20;&#x7684;&#x5143;&#x7EC4;&#x6A21;&#x5F0F;&#x3002;&#x5173;&#x4E8E;&#x4F7F;&#x7528; <code>switch</code> &#x8BED;&#x53E5;&#x6765;&#x5339;&#x914D;&#x5305;&#x542B;&#x5173;&#x8054;&#x503C;&#x7684;&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/08_Enumerations.html#associated_values">&#x5173;&#x8054;&#x503C;</a>&#x3002;</p>
<blockquote>
<p>&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="enum-case-pattern">enum-case-pattern </h4>
<blockquote>
<p><em>&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;</em> &#x2192; <a href="03_Types.html#type-identifier"><em>&#x7C7B;&#x578B;&#x6807;&#x8BC6;</em></a><sub>&#x53EF;&#x9009;</sub> <strong>.</strong> <a href="06_Declarations.html#enum-case-name"><em>&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x540D;</em></a> <a href="#tuple-pattern"><em>&#x5143;&#x7EC4;&#x6A21;&#x5F0F;</em></a><sub>&#x53EF;&#x9009;</sub></p>
</blockquote>
<h2 id="optional-pattern">&#x53EF;&#x9009;&#x6A21;&#x5F0F;&#xFF08;Optional Pattern&#xFF09; </h2>
<p><em>&#x53EF;&#x9009;&#x6A21;&#x5F0F;</em>&#x5339;&#x914D;&#x5305;&#x88C5;&#x5728;&#x4E00;&#x4E2A; <code>Optional(Wrapped)</code> &#x6216;&#x8005; <code>ExplicitlyUnwrappedOptional(Wrapped)</code> &#x679A;&#x4E3E;&#x4E2D;&#x7684; <code>Some(Wrapped)</code> &#x7528;&#x4F8B;&#x4E2D;&#x7684;&#x503C;&#x3002;&#x53EF;&#x9009;&#x6A21;&#x5F0F;&#x7531;&#x4E00;&#x4E2A;&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;&#x548C;&#x7D27;&#x968F;&#x5176;&#x540E;&#x7684;&#x4E00;&#x4E2A;&#x95EE;&#x53F7;&#x7EC4;&#x6210;&#xFF0C;&#x53EF;&#x4EE5;&#x50CF;&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;&#x4E00;&#x6837;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x7531;&#x4E8E;&#x53EF;&#x9009;&#x6A21;&#x5F0F;&#x662F; <code>Optional</code> &#x548C; <code>ImplicitlyUnwrappedOptional</code> &#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;&#x7684;&#x8BED;&#x6CD5;&#x7CD6;&#xFF0C;&#x4E0B;&#x9762;&#x4E24;&#x79CD;&#x5199;&#x6CD5;&#x662F;&#x7B49;&#x6548;&#x7684;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> someOptional: <span class="hljs-type">Int</span>? = <span class="hljs-number">42</span>
<span class="hljs-comment">// &#x4F7F;&#x7528;&#x679A;&#x4E3E;&#x7528;&#x4F8B;&#x6A21;&#x5F0F;&#x5339;&#x914D;</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">case</span> .<span class="hljs-type">Some</span>(<span class="hljs-keyword">let</span> x) = someOptional {
    <span class="hljs-built_in">print</span>(x)
}

<span class="hljs-comment">// &#x4F7F;&#x7528;&#x53EF;&#x9009;&#x6A21;&#x5F0F;&#x5339;&#x914D;</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> x? = someOptional {
    <span class="hljs-built_in">print</span>(x)
}
</code></pre>
<p>&#x53EF;&#x9009;&#x6A21;&#x5F0F;&#x4E3A; <code>for-in</code> &#x8BED;&#x53E5;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x8FED;&#x4EE3;&#x6570;&#x7EC4;&#x7684;&#x7B80;&#x4FBF;&#x65B9;&#x5F0F;&#xFF0C;&#x53EA;&#x4E3A;&#x6570;&#x7EC4;&#x4E2D;&#x975E; <code>nil</code> &#x7684;&#x5143;&#x7D20;&#x6267;&#x884C;&#x5FAA;&#x73AF;&#x4F53;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> arrayOfOptionalInts: [<span class="hljs-type">Int</span>?] = [<span class="hljs-literal">nil</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-literal">nil</span>, <span class="hljs-number">5</span>]
<span class="hljs-comment">// &#x53EA;&#x5339;&#x914D;&#x975E; nil &#x7684;&#x5143;&#x7D20;</span>
<span class="hljs-keyword">for</span> <span class="hljs-keyword">case</span> <span class="hljs-keyword">let</span> number? <span class="hljs-keyword">in</span> arrayOfOptinalInts {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Found a <span class="hljs-subst">\(number)</span>&quot;</span>)
}
<span class="hljs-comment">// Found a 2</span>
<span class="hljs-comment">// Found a 3</span>
<span class="hljs-comment">// Found a 5</span>
</code></pre>
<blockquote>
<p>&#x53EF;&#x9009;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="optional-pattern">optional-pattern </h4>
<blockquote>
<p><em>&#x53EF;&#x9009;&#x6A21;&#x5F0F;</em> &#x2192; <a href="03_Types.html#type-identifier"><em>&#x6807;&#x8BC6;&#x7B26;&#x6A21;&#x5F0F;</em></a> <strong>?</strong></p>
</blockquote>
<h2 id="type-casting-patterns">&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x6A21;&#x5F0F;&#xFF08;Type-Casting Patterns&#xFF09; </h2>
<p>&#x6709;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x6A21;&#x5F0F;&#xFF0C;<code>is</code> &#x6A21;&#x5F0F;&#x548C; <code>as</code> &#x6A21;&#x5F0F;&#x3002;<code>is</code> &#x6A21;&#x5F0F;&#x53EA;&#x51FA;&#x73B0;&#x5728; <code>switch</code> &#x8BED;&#x53E5;&#x4E2D;&#x7684; <code>case</code> &#x6807;&#x7B7E;&#x4E2D;&#x3002;<code>is</code> &#x6A21;&#x5F0F;&#x548C; <code>as</code> &#x6A21;&#x5F0F;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>is <code>&#x7C7B;&#x578B;</code></p>
<p><code>&#x6A21;&#x5F0F;</code> as <code>&#x7C7B;&#x578B;</code></p>
</blockquote>
<p><code>is</code> &#x6A21;&#x5F0F;&#x4EC5;&#x5F53;&#x4E00;&#x4E2A;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x548C; <code>is</code> &#x6A21;&#x5F0F;&#x53F3;&#x8FB9;&#x7684;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x4E00;&#x81F4;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x5176;&#x5B50;&#x7C7B;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x624D;&#x4F1A;&#x5339;&#x914D;&#x8FD9;&#x4E2A;&#x503C;&#x3002;<code>is</code> &#x6A21;&#x5F0F;&#x548C; <code>is</code> &#x8FD0;&#x7B97;&#x7B26;&#x6709;&#x76F8;&#x4F3C;&#x8868;&#x73B0;&#xFF0C;&#x5B83;&#x4EEC;&#x90FD;&#x8FDB;&#x884C;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#xFF0C;&#x4F46;&#x662F; <code>is</code> &#x6A21;&#x5F0F;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x7C7B;&#x578B;&#x3002;</p>
<p><code>as</code> &#x6A21;&#x5F0F;&#x4EC5;&#x5F53;&#x4E00;&#x4E2A;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x548C; <code>as</code> &#x6A21;&#x5F0F;&#x53F3;&#x8FB9;&#x7684;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x4E00;&#x81F4;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x5176;&#x5B50;&#x7C7B;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x624D;&#x4F1A;&#x5339;&#x914D;&#x8FD9;&#x4E2A;&#x503C;&#x3002;&#x5982;&#x679C;&#x5339;&#x914D;&#x6210;&#x529F;&#xFF0C;&#x88AB;&#x5339;&#x914D;&#x7684;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x88AB;&#x8F6C;&#x6362;&#x6210; <code>as</code> &#x6A21;&#x5F0F;&#x53F3;&#x8FB9;&#x6307;&#x5B9A;&#x7684;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x5173;&#x4E8E;&#x4F7F;&#x7528; <code>switch</code> &#x8BED;&#x53E5;&#x914D;&#x5408; <code>is</code> &#x6A21;&#x5F0F;&#x548C; <code>as</code> &#x6A21;&#x5F0F;&#x6765;&#x5339;&#x914D;&#x503C;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="../chapter2/18_Type_Casting.html#type_casting_for_any_and_anyobject">Any &#x548C; AnyObject &#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362;</a>&#x3002;</p>
<blockquote>
<p>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="type-casting-pattern">type-casting-pattern </h4>
<blockquote>
<p><em>&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x6A21;&#x5F0F;</em> &#x2192; <a href="#is-pattern"><em>is &#x6A21;&#x5F0F;</em></a> | <a href="#as-pattern"><em>as &#x6A21;&#x5F0F;</em></a></p>
</blockquote>
<h4 id="is-pattern">is-pattern </h4>
<blockquote>
<p><em>is &#x6A21;&#x5F0F;</em> &#x2192; <strong>is</strong> <a href="03_Types.html#type"><em>&#x7C7B;&#x578B;</em></a></p>
</blockquote>
<h4 id="as-pattern">as-pattern </h4>
<blockquote>
<p><em>as &#x6A21;&#x5F0F;</em> &#x2192; <a href="#pattern"><em>&#x6A21;&#x5F0F;</em></a> <strong>as</strong> <a href="03_Types.html#type"><em>&#x7C7B;&#x578B;</em></a></p>
</blockquote>
<h2 id="expression-pattern">&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;&#xFF08;Expression Pattern&#xFF09; </h2>
<p><em>&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;</em>&#x4EE3;&#x8868;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x3002;&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;&#x53EA;&#x51FA;&#x73B0;&#x5728; <code>switch</code> &#x8BED;&#x53E5;&#x4E2D;&#x7684; <code>case</code> &#x6807;&#x7B7E;&#x4E2D;&#x3002;</p>
<p>&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;&#x4EE3;&#x8868;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x4F7F;&#x7528; Swift &#x6807;&#x51C6;&#x5E93;&#x4E2D;&#x7684; <code>~=</code> &#x8FD0;&#x7B97;&#x7B26;&#x4E0E;&#x8F93;&#x5165;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x3002;&#x5982;&#x679C; <code>~=</code> &#x8FD0;&#x7B97;&#x7B26;&#x8FD4;&#x56DE; <code>true</code>&#xFF0C;&#x5219;&#x5339;&#x914D;&#x6210;&#x529F;&#x3002;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;<code>~=</code> &#x8FD0;&#x7B97;&#x7B26;&#x4F7F;&#x7528; <code>==</code> &#x8FD0;&#x7B97;&#x7B26;&#x6765;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x76F8;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x3002;&#x5B83;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x4E00;&#x4E2A;&#x6574;&#x578B;&#x6570;&#x503C;&#x4E0E;&#x4E00;&#x4E2A; <code>Range</code> &#x5B9E;&#x4F8B;&#x4E2D;&#x7684;&#x4E00;&#x6BB5;&#x6574;&#x6570;&#x533A;&#x95F4;&#x505A;&#x5339;&#x914D;&#xFF0C;&#x6B63;&#x5982;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> point = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
<span class="hljs-keyword">switch</span> point {
<span class="hljs-keyword">case</span> (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>):
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;(0, 0) is at the origin.&quot;</span>)
<span class="hljs-keyword">case</span> (-<span class="hljs-number">2</span>...<span class="hljs-number">2</span>, -<span class="hljs-number">2</span>...<span class="hljs-number">2</span>):
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;(<span class="hljs-subst">\(point.<span class="hljs-number">0</span>)</span>, <span class="hljs-subst">\(point.<span class="hljs-number">1</span>)</span>) is near the origin.&quot;</span>)
<span class="hljs-keyword">default</span>:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The point is at (<span class="hljs-subst">\(point.<span class="hljs-number">0</span>)</span>, <span class="hljs-subst">\(point.<span class="hljs-number">1</span>)</span>).&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;(1, 2) is near the origin.&#x201D;</span>
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x91CD;&#x8F7D; <code>~=</code> &#x8FD0;&#x7B97;&#x7B26;&#x6765;&#x63D0;&#x4F9B;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x5339;&#x914D;&#x884C;&#x4E3A;&#x3002;&#x6BD4;&#x5982;&#x4F60;&#x53EF;&#x4EE5;&#x91CD;&#x5199;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5C06; <code>point</code> &#x8868;&#x8FBE;&#x5F0F;&#x4E0E;&#x5B57;&#x7B26;&#x4E32;&#x5F62;&#x5F0F;&#x8868;&#x793A;&#x7684;&#x70B9;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x3002;</p>
<pre><code class="lang-swift"><span class="hljs-comment">// &#x91CD;&#x8F7D; ~= &#x8FD0;&#x7B97;&#x7B26;&#x5BF9;&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x6574;&#x6570;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> ~=<span class="hljs-params">(pattern: String, value: Int)</span></span> -&gt; <span class="hljs-type">Bool</span> {
    <span class="hljs-keyword">return</span> pattern == <span class="hljs-string">&quot;<span class="hljs-subst">\(value)</span>&quot;</span>
}

<span class="hljs-keyword">switch</span> point {
<span class="hljs-keyword">case</span> (<span class="hljs-string">&quot;0&quot;</span>, <span class="hljs-string">&quot;0&quot;</span>):
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;(0, 0) is at the origin.&quot;</span>)
<span class="hljs-keyword">default</span>:
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;The point is at (<span class="hljs-subst">\(point.<span class="hljs-number">0</span>)</span>, <span class="hljs-subst">\(point.<span class="hljs-number">1</span>)</span>).&quot;</span>)
}
<span class="hljs-comment">// &#x6253;&#x5370;&#x201C;The point is at (1, 2).&#x201D;</span>
</code></pre>
<blockquote>
<p>&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;</p>
</blockquote>
<h4 id="expression-pattern">expression-pattern </h4>
<blockquote>
<p><em>&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;</em> &#x2192; <a href="04_Expressions.html#expression"><em>&#x8868;&#x8FBE;&#x5F0F;</em></a></p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="07_Attributes.html" class="navigation navigation-prev " aria-label="Previous page: 特性">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="09_Generic_Parameters_and_Arguments.html" class="navigation navigation-next " aria-label="Next page: 泛型参数">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"模式","level":"1.4.8","depth":2,"next":{"title":"泛型参数","level":"1.4.9","depth":2,"path":"chapter3/09_Generic_Parameters_and_Arguments.md","ref":"chapter3/09_Generic_Parameters_and_Arguments.md","articles":[]},"previous":{"title":"特性","level":"1.4.7","depth":2,"path":"chapter3/07_Attributes.md","ref":"chapter3/07_Attributes.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter3/08_Patterns.md","mtime":"2019-07-05T01:01:08.280Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-05T02:41:17.672Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src=""></script>
    <script src=""></script>
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    
        
        <script src=""></script>
        
    

    </body>
</html>

